package com.itheima.reggie.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Transactional //多表操作，开启事务
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private  SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public R saveWithDish(@RequestBody SetmealDto setmealDto) {
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (CollUtil.isEmpty(setmealDishes)){
            return R.error("套餐中菜品数据为空，请重新选择！");
        }
        //1、保存套餐数据到 setmeal表中 ： SetmealMapper
        setmealMapper.insert(setmealDto);
        //2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
        //2.1 获取当前套餐的菜品列表
        //2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        for (SetmealDish setmealDish : setmealDishes) {
            //2.3 获取套餐id，并set到setmealDish中
            setmealDish.setSetmealId(setmealDto.getId());
            //2.4 保存套餐菜品数据 setmealDish
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("新增套餐成功");
    }

    @Override
    public R updateWithDish(@RequestBody SetmealDto setmealDto) {
        //1、根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        setmealMapper.updateById(setmealDto);
        //2、操作套餐对应的菜品数据： setmeal_dish
        //2.1 先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?
        // 获取套餐id   setmealDto.getId()
        Long setmealId = setmealDto.getId();
        // 使用 setmealDishMapper 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        LambdaUpdateWrapper<SetmealDish> setmealDishLqw = new LambdaUpdateWrapper<>();
        setmealDishLqw.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishMapper.delete(setmealDishLqw);
        //2.2 重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        // setmealDto.getSetmealDishes() 获取套餐的菜品集合 List<SetmealDish>
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 遍历套餐菜品集合数据
        for (SetmealDish setmealDish : setmealDishes) {
            // 有没有数据需要set ： 设置套餐id  setSetmealId
            setmealDish.setSetmealId(setmealDto.getId());
            // 添加套餐菜品数据 insert
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改套餐成功");
    }

    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //1、根据套餐id查询套餐数据 select * from setmeal where id =?
        Setmeal setmeal = setmealMapper.selectById(id);
        //2、根据套餐id查询套餐对应的菜品数据 select * from setmeal_dish where setmeal_id = ?
        LambdaUpdateWrapper<SetmealDish> setmealDishLqw = new LambdaUpdateWrapper<>();
        setmealDishLqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(setmealDishLqw);
        //3、将套餐 setmeal 和套餐菜品集合 setmealDishList 数据封装到 SetmealDto并返回
        // 3.0 首先new一个SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        // 3.1、BeanUtils.copyProperties() 拷贝setmeal到setmealDto中
        BeanUtils.copyProperties(setmeal, setmealDto);
        // 3.2、将setmealDishList 设置到 setmealDto中
        setmealDto.setSetmealDishes(setmealDishes);
        //4、返回 setmealDto
        return setmealDto;
    }

}
